package com.fenghongzhang.a1808;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btn1(View view) {
        //AlertDialog
        /**
         * 专门用来构造对话框的.
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("标题");
        builder.setMessage("内容").setIcon(R.mipmap.ic_launcher);

        //确定
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        
        builder.setNegativeButton("不Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });

        //之前
        AlertDialog alertDialog = builder.create();

        //实现对话框
        alertDialog.show();

    }

    public void btn2(View view) {
        //AlertDialog
        /**
         * 专门用来构造对话框的.
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("标题");
        builder.setIcon(R.mipmap.ic_launcher);
         final String[] str = new String[]{"男人","女人","不男不女"};

        //单选
        builder.setSingleChoiceItems(str, 1, new DialogInterface.OnClickListener() {
            /**
             * 1.7
             * @param dialog
             * @param which 数组的下标
             */
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show();
            }
        });

        //确定
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("不Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });

        //之前
        AlertDialog alertDialog = builder.create();

        //实现对话框
        alertDialog.show();

    }

    public void btn3(View view) {

        //AlertDialog
        /**
         * 专门用来构造对话框的.
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("标题");
        builder.setIcon(R.mipmap.ic_launcher);


        final String[] strings = new String[]{"篮球","足球","电影"};

        final boolean[] booleans = new boolean[]{true,true,false};

        //多选
        builder.setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
            /**
             *
             * @param dialog
             * @param which
             * @param isChecked  是否被选中
             */
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                //选中的时候,吐司一下
                if(isChecked){
                    Toast.makeText(MainActivity.this, strings[which]+"被选中", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, strings[which]+"被取消", Toast.LENGTH_SHORT).show();
                }

                //每一次点击以后, 都遍历输出已经选择的项目
//                for (int i = 0; i < booleans.length; i++) {
//                    if(booleans[i]){
//                        Toast.makeText(MainActivity.this, strings[i], Toast.LENGTH_SHORT).show();
//                    }
//                }
            }
        });

        //确定
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("不Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });

        //之前
        AlertDialog alertDialog = builder.create();

        //实现对话框
        alertDialog.show();
    }

    public void btn4(View view) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher_background);//设置图标
        builder.setTitle("hello body");//设置标题
        //设置确定按钮
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
            }
        });
        //设置取消按钮
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
            }
        });
        //把一个xml -> view
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
        ImageView viewById = inflate.findViewById(R.id.dialog_img);
        //自定义内容
        builder.setView(inflate);
        final AlertDialog dialog = builder.create();
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();

    }

    public void btn5(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //应该是什么?
        progressDialog.setMax(100);
        progressDialog.setMessage("进度");
        progressDialog.show();
        progressDialog.setProgress(0);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                index += 10;
                progressDialog.setProgress(index);
                if(index >= 100){
                    timer.cancel();
//                    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                }
            }
        },0,1000);


//        Log.i(TAG, "btn5: ");

        //定时器
        /**
         * 1,任务
         * 2,第一次的延迟
         * 3.间隔时间执行
         */
//        Timer timer = new Timer(); //闹钟
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                Log.i(TAG, "run: ");
//            }
//        },0,2000);
//
//        //停止
//        timer.cancel();

    }

    public void btn6(View view) {

        Calendar instance = Calendar.getInstance();

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            /**
             *
             * @param view
             * @param year
             * @param month 0 --11
             * @param dayOfMonth
             */
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, year+":"+(month+1)+":"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        }, instance.get(Calendar.YEAR), instance.get(Calendar.MONTH), instance.get(Calendar.DAY_OF_MONTH));

        datePickerDialog.show();
    }

    public void btn7(View view) {

        Calendar instance = Calendar.getInstance();
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        }, instance.get(Calendar.HOUR), instance.get(Calendar.MINUTE), false).show();
    }


    public void btn8(View view) {

        /**
         * 1. 对话的自定义样式
         * 2. 继承dialog
         * 3. 把布局和对话框绑定
         *  setContentView(R.layout.cust_layout);
         * 4. onCreate find
         * 5.定义中间字符串
         * 6.给组件赋值
         * 7. 在外部创建对话
         */
        MyDialog myDialog = new MyDialog(this);
        myDialog.setTitleStr("biaobiaotiiii");
        myDialog.setMsgStr("哈哈哈哈哈");
        myDialog.setYesStr("ok");
        
        
//        myDialog.setONONONOlist
        myDialog.setYesOnClickListener(new MyDialog.YesOnClickListener() {
            @Override
            public void click() {
                Toast.makeText(MainActivity.this, "hahahaha", Toast.LENGTH_SHORT).show();
            }
        });


        //5
        myDialog.setNoClickListener(new MyDialog.NoClickListener() {
            @Override
            public void click() {
                Toast.makeText(MainActivity.this, "吃吃此航次", Toast.LENGTH_SHORT).show();

            }
        });
        myDialog.show();

    }
}
